# Archlive <http://archlive-pkg.googlecode.com>

pkgbase="kernel26"
pkgname=('kernel26' 'kernel26-headers' 'linux-api-headers' 
	#'kernel26-docs'   #編譯意義不大
	)
_basekernel=2.6.38
pkgver=2.6.38.4
pkgrel=1
arch=(i686 x86_64)
license=('GPL2')
url="http://www.kernel.org"
options=(!strip)
arch_patchname="patch-${pkgver}-3-ARCH"

_realpkgname=kernel26
_configure_as_ARCH_way=y
_kernelname=${_realpkgname#kernel26}
## if you want your own version, such as 2.6.31-archlive, then 
## "_configure_as_ARCH_way" set to "1" and
## "_realpkgname" change to "kernel26-archlive"
_include_squashfs=y
# 是否保存编译目录
_keep_build_dir=y
## More special selection as below items.
#
makedepends=('gcc'
	# 'xmlto' 'docbook-xsl'
	)
source=(kernel26.preset config.i686 config.x86_64 \
	http://www.kernel.org/pub/linux/kernel/v2.6/linux-$pkgver.tar.bz2
	#ftp://ftp.archlinux.org/other/kernel26/${arch_patchname}.bz2
	http://sources.gentoo.org/cgi-bin/viewvc.cgi/linux-patches/genpatches-2.6/trunk/2.6.38/4200_fbcondecor-0.9.6.patch
	### Chinese Characters Patches 孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/
	http://zdbr.net.cn/download/utf8-kernel-2.6.38-core-1.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6.31-fbcondecor-1.patch.bz2
	http://zdbr.net.cn/download/utf8-kernel-2.6-fonts-3.patch.bz2
	aufs2-base.patch aufs2-standalone.patch
	# BFS
	http://ck.kolivas.org/patches/bfs/2.6.38/2.6.38.3-sched-bfs-401.patch
	# reiser4
	http://www.kernel.org/pub/linux/kernel/people/edward/reiser4/reiser4-for-2.6/reiser4-for-2.6.38.patch.gz
)

####################################################################
# KERNEL CONFIG FILES
# This PKGBUILD searches for config files in the current directory
# and will use the first one it finds from the following
# list as base configuration:
# 	config.local
# 	config.saved.$CARCH
# 	config.$CARCH
####################################################################
# PATCHES
# This package builds the vanilla git kernel by default,
# but it is possible to patch the source without modifying
# this PKGBUILD.
# Simply create a directory 'patches' in your PKGBUILD
# directory and _any_ file (dotfiles excluded) in this
# folder will be applied to the kernel source.
# Prefixing the patch file names with dots will obviously
# excluded them from the patching process.
#############################################################
# CONFIGURATION
# Uncomment desired options
#######
# Set to menuconfig, xconfig, gconfig or oldconfig
# If you set it to something else, the
# (manual) kernel configuration step will
# be skipped
_menu_method="menuconfig"	
#######
# The directory where the kernel should be built
# Can be useful, for example, if you want to compile on a
# tmpfs mount, which can speed up the compilation process
_build_dir="$srcdir"
#######
# Stop build process after kernel configuration
# This option enables _save_config implicitly.
# _configure_only=1
#######
# Save the .config file to package directory
# as config.saved.$CARCH
# _save_config=1
#######
# Make the kernel build process verbose
# _verbose=1
####################################################################

extrapatch() {
  msg2 "Add Gentoo patches 加入Gentoo官方基本补丁..."
  for i in $(ls $srcdir/4[0-9][0-9][0-9]*);do
    	echo "Applying $(basename $i)" 
  	patch -Np1 -i $i || return 1
  done

  msg2 "Add the kernel Chinese characters patches 加入孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/"
  patch -Np1 -i $srcdir/utf8*core*.patch || return 1
  patch -Np1 -i $srcdir/utf8*fbcondecor*.patch || return 1
  patch -Np1 -i $srcdir/utf8*fonts*.patch || return 1

  # Add -ARCH patches
  # See http://projects.archlinux.org/linux-2.6-ARCH.git/
  #patch -Np1 -i ${srcdir}/${arch_patchname}

  # Add aufs2 patches
  patch -Np1 -i ${srcdir}/aufs2-base.patch
  patch -Np1 -i ${srcdir}/aufs2-standalone.patch

  # Add reiser4 patches
  patch -Np1 -i ${srcdir}/reiser4-for-2.6.38.patch
}

prepare() {
	KARCH="x86"
	cd ${srcdir}/linux-$_basekernel

	# Add Arch patch, Gentoo patche, and kernel Chinese characters patches.
	extrapatch

	# Apply other patches
	if [ -d "$startdir/patches" ]; then
		msg "Applying patches..."
		for i in "$startdir/patches/"*; do
			msg2 "Applying ${i##*/}..."
			patch -Np1 -i "$i" || (error "Applying ${i##*/} failed" && return 1)
		done
	fi

	#################
	# CONFIGURATION
	#################
	msg "Loading configuration..."
	for i in local "saved.$CARCH" "$CARCH"; do
		if [ -e "$startdir/config.$i" ]; then
			msg2 "Using kernel config file config.$i..."
			cp -f "$startdir/config.$i" .config || return 1
			break
		fi
	done
	[ ! -e .config ] &&
		warning "No suitable kernel config file was found. You'll have to configure the kernel from scratch."

	###########################
	# Configure as ARCH way
	###########################
	if [ "$_configure_as_ARCH_way" = "y" ]; then
		msg "Configure the kernel version as ARCH way, like 2.6.31-ARCH"
		if [ "${_kernelname}" != "" ]; then local_version=${_kernelname}; else local_version="-ARCH"; fi
		sed -i "s|^EXTRAVERSION = .*$|EXTRAVERSION =|g" Makefile
		sed -i "s|.*CONFIG_LOCALVERSION=.*|CONFIG_LOCALVERSION=\"${local_version}\"|g" .config
		sed -i "s|.*CONFIG_LOCALVERSION_AUTO.*|\# CONFIG_LOCALVERSION_AUTO is not set|g" .config
	fi
	# The default .config without support for squashfs, can change it by set "_include_squashfs" as "y".
	if [ "$_include_squashfs" = "y" ]; then
		msg "Selected squashfs4 included in kernel package."
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" = "x" ]; then
			sed -i "s|.*CONFIG_SQUASHFS.*|CONFIG_SQUASHFS=m\
				\n# CONFIG_SQUASHFS_EMBEDDED is not set\
				\nCONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE=3|g" ./.config
		fi
	else
		if [ "x$(grep "CONFIG_SQUASHFS=m" .config)" != "x" ]; then
			sed -i "s|CONFIG_SQUASHFS=m|\# CONFIG_SQUASHFS is not set|g" .config
		fi
	fi

	make prepare
	msg "Update .config Finished"

	###########################
	# Start the configuration
	###########################
	msg "Updating configuration..."

	yes "" | make config > /dev/null

	# Save the config file to the package directory
	if [[ -n "$_save_config" || -n "$_configure_only" ]]; then
		msg "Saving configuration..."
		msg2 "Saving $_build_dir/$_gitname-build/.config as $startdir/config.saved.$CARCH"
		cp .config "$startdir/config.saved.$CARCH" || return 1
	fi

	# Stop after configuration if desired
	if [ -n "$_configure_only" ]; then
		rm -rf "$_build_dir/$_gitname-build"
		return 1
	fi
	#################
	# BUILD PROCESS
	#################

	msg "Checking the kernel version..."
	_kernver="$(make kernelrelease)"
	if [ "$_configure_as_ARCH_way" = "y" ]; then
		# Check the _kernver again, if it is your wanted (Special for _configure_as_ARCH_way)
		_localversion=$(grep CONFIG_LOCALVERSION= .config | cut -d "\"" -f 2)
		if [ "${_kernver#*-}" != ${_localversion#-} ]; then
			msg "There are some errors when get your own kernel version."
			msg "_kernver=${_kernver}, _localversion=${_localversion}."
			exit 1
		else
			msg "Get the right kernel version as ${_kernver}"
		fi
	fi
}

build () {

	#prepare
	msg "Building kernel and modules..."

	cd ${srcdir}/linux-$_basekernel
	make ${MAKEFLAGS} bzImage modules
	msg "Compile finished!"
}

package_kernel26(){
	pkgdesc="The Linux Kernel and modules"
	groups=('base')
	provides=("kernel26=$_basekernel" "kernel26=${_basekernel}-ARCH")
	conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi' 'kernel26' 'unionfs')
	depends=('coreutils' 'module-init-tools>=3.12-2' 'mkinitcpio>=0.6.8-2')
	optdepends=('crda: to set the correct wireless channels of your country')

	replaces=('kernel24' 'kernel24-scsi' 'kernel26-scsi'
            'alsa-driver' 'ieee80211' 'hostap-driver26'
            'pwc' 'nforce' 'squashfs' 'unionfs' 'ivtv'
            'zd1211' 'kvm-modules' 'iwlwifi' 'rt2x00-cvs'
            'gspcav1' 'atl2' 'wlan-ng26' 'rt2500' 'nouveau-drm')
 	backup=(etc/mkinitcpio.d/${pkgname}.preset)
	install=kernel26.install

	KARCH="x86"
	cd ${srcdir}/linux-$_basekernel
	_kernver="$(make kernelrelease)"

	mkdir -p ${pkgdir}/{lib/modules,lib/firmware,boot}

	msg "Installing kernel modules..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install

	msg "Installing kernel image..."
	install -Dm644 System.map  "$pkgdir/boot/System.map26${_kernelname}"
	install -Dm644 arch/$KARCH/boot/bzImage "$pkgdir/boot/vmlinuz26${_kernelname}"
	install -Dm644 .config  "$pkgdir/boot/kconfig26"

	#msg "add vmlinux"
	#install -m644 -D vmlinux "${pkgdir}/usr/src/linux-${_kernver}/vmlinux"

	msg "Installing preset file..."
	install -Dm644 "$srcdir/kernel26.preset" \
		"$pkgdir/etc/mkinitcpio.d/kernel26.preset" || return 1
	sed \
		-e "s|source .*|source /etc/mkinitcpio.d/kernel26${_kernelname}.kver|g" \
		-e "s|default_image=.*|default_image=\"/boot/${pkgname}.img\"|g" \
		-e "s|fallback_image=.*|fallback_image=\"/boot/${pkgname}-fallback.img\"|g" \
		-i ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset

	msg "Update version on install file"
	sed \
		-e  "s/KERNEL_NAME=.*/KERNEL_NAME=${_kernelname}/g" \
		-e  "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" \
		-i $startdir/kernel26.install

	msg "Generating kernel26.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/kernel26.kver" || return 1

	msg "remove build and source links"
	rm -f ${pkgdir}/lib/modules/${_kernver}/{source,build}

	msg "Remove the firmware..."
	rm -rf "$pkgdir/lib/firmware"

	msg "gzip -9 all modules to safe 100MB of space"
	find "$pkgdir" -name '*.ko' -exec gzip -9 {} \;

	msg "Kernel and modules installing finished."
}

package_kernel26-headers() {
	pkgdesc="Header files and scripts for building modules for kernel26"

	mkdir -p ${pkgdir}/lib/modules/${_kernver}
	cd ${pkgdir}/lib/modules/${_kernver}
	ln -sf ../../../usr/src/linux-${_kernver} build

	cd ${srcdir}/linux-$_basekernel
	install -D -m644 Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Makefile
	install -D -m644 kernel/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/kernel/Makefile
	install -D -m644 .config \
		${pkgdir}/usr/src/linux-${_kernver}/.config
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include

	for i in acpi asm-generic config generated linux math-emu media net pcmcia scsi sound trace video xen; do
		cp -a include/$i ${pkgdir}/usr/src/linux-${_kernver}/include/
	done

	msg "copy arch includes for external modules"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/x86
	cp -a arch/x86/include ${pkgdir}/usr/src/linux-${_kernver}/arch/x86/

	msg "copy files necessary for later builds, like nvidia and vmware"
	cp Module.symvers ${pkgdir}/usr/src/linux-${_kernver}
	cp -a scripts ${pkgdir}/usr/src/linux-${_kernver}

	msg "fix permissions on scripts dir"
	chmod og-w -R ${pkgdir}/usr/src/linux-${_kernver}/scripts
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/.tmp_versions

	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel

	cp arch/$KARCH/Makefile ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	if [ "$CARCH" = "i686" ]; then
		cp arch/$KARCH/Makefile_32.cpu ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	fi
	cp arch/$KARCH/kernel/asm-offsets.s ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel/

	msg "add headers for lirc package"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video
	cp drivers/media/video/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/
	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvision; do
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
		cp -a drivers/media/video/$i/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
	done
	
	msg "add docbook makefile"
	install -D -m644 Documentation/DocBook/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile
	msg "add dm headers"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
	cp drivers/md/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
	
	msg "add inotify.h"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/linux
	cp include/linux/inotify.h ${pkgdir}/usr/src/linux-${_kernver}/include/linux/

	msg "add wireless headers"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/
	cp net/mac80211/*.h ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/

	msg "add dvb headers for external modules"
	# in reference to:
	# http://bugs.archlinux.org/task/9912
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core
	cp drivers/media/dvb/dvb-core/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core/
	
	msg "add dvb headers for external modules"
	# in reference to:
	# http://bugs.archlinux.org/task/11194
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
	cp include/config/dvb/*.h ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
	
	msg "add dvb headers"
	# for http://mcentral.de/hg/~mrec/em28xx-new"
	# in reference to:
	# http://bugs.archlinux.org/task/13146
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/dvb/frontends/lgdt330x.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/video/msp3400-driver.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	# add dvb headers  
	# in reference to:
	# http://bugs.archlinux.org/task/20402
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb
	cp drivers/media/dvb/dvb-usb/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb/
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends
	cp drivers/media/dvb/frontends/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners
	cp drivers/media/common/tuners/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners/

	msg "add xfs and shmem for aufs building"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/mm
	cp fs/xfs/xfs_sb.h ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h

	msg "add headers vor virtualbox"
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/drm $pkgdir/usr/src/linux-${_kernver}/include/

	msg "add headers for broadcom wl"
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/trace $pkgdir/usr/src/linux-${_kernver}/include/

	msg "add headers for crypto modules"
	# in reference to:
	# http://bugs.archlinux.org/task/22081
	cp -a include/crypto $pkgdir/usr/src/linux-${_kernver}/include/

	msg "copy in Kconfig files"
	for i in `find . -name "Kconfig*"`; do 
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/`echo $i | sed 's|/Kconfig.*||'`
		cp $i ${pkgdir}/usr/src/linux-${_kernver}/$i
	done

	chown -R root.root ${pkgdir}/usr/src/linux-${_kernver}
	find ${pkgdir}/usr/src/linux-${_kernver} -type d -exec chmod 755 {} \;

	msg "remove unneeded architectures"
	rm -rf ${pkgdir}/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa}
}

package_kernel26-docs() {
	pkgdesc="Kernel hackers manual - HTML documentation that comes with the Linux kernel."

	cd ${srcdir}/linux-$_basekernel

	mkdir -p $pkgdir/usr/src/linux-$_kernver
	mv Documentation $pkgdir/usr/src/linux-$_kernver
	find $pkgdir -type f -exec chmod 444 {} \;
	find $pkgdir -type d -exec chmod 755 {} \;
	# remove a file already in kernel26 package
	rm -f $pkgdir/usr/src/linux-$_kernver/Documentation/DocBook/Makefile
}

package_linux-api-headers() {
	pkgdesc="Kernel headers sanitized for use in userspace"
	url="http://www.gnu.org/software/libc"
	provides=("kernel-headers=${pkgver}")
	conflicts=('kernel-headers')
	replaces=('kernel-headers')

	cd ${srcdir}/linux-$_basekernel
	make INSTALL_HDR_PATH=${pkgdir}/usr headers_install

	# use headers from libdrm
	rm -rf ${pkgdir}/usr/include/drm
  
	# clean-up unnecessary files generated during install
	find ${pkgdir} -name .install -or -name ..install.cmd | xargs rm -f
}
